package org.javaweb.cms_web.service;

import org.javaweb.cms_web.mapper.AchievementMapper;
import org.javaweb.cms_web.model.Achievement;
import org.javaweb.cms_web.model.Enum.ContentStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Service
public class AchievementService {

    @Autowired
    private AchievementMapper achievementMapper;

    //处理文件路径的斜杠
    public String prepareUrl(String originalPath) {
        if (originalPath == null || originalPath.isEmpty()) {
            return "";
        }

        // 将反斜杠替换为正斜杠
        String normalizedPath = originalPath.replace("\\", "/");
        try {
            // 对路径进行 URL 编码
            return URLEncoder.encode(normalizedPath, StandardCharsets.UTF_8.name());
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    // 根据团队ID查询科研成果
    public List<Achievement> getAchievementsByTeam(int teamID, Integer category) {
        return achievementMapper.findAchievementsByTeam(teamID, category);
    }

    // 根据团队ID和标题模糊查询科研成果
    public List<Achievement> getAchievementsByTeamAndTitle(int teamID, String title, Integer category) {
        return achievementMapper.findAchievementsByTeamAndTitle(teamID, title, category);
    }

    // 新增科研成果
    public boolean createAchievement(Achievement achievement) {
        // 在插入之前，你可以进行其他验证或处理逻辑
        return achievementMapper.insert(achievement) > 0;
    }


    // 删除指定ID的科研成果
    public boolean deleteAchievement(Integer achievementId) {
        return achievementMapper.deleteById(achievementId) > 0;
    }

    // 更新成果的可见性（公开或隐藏）
    public boolean updateVisibility(Integer achievementId, int visibility) {
        return achievementMapper.updateVisibility(achievementId, visibility) > 0;
    }

    // 获取指定ID的成果
    public Achievement getAchievementById(Integer achievementId) {
        return achievementMapper.findById(achievementId);
    }

    // 更新成果的内容
    public boolean updateAchievement(Achievement achievement) {
        return achievementMapper.update(achievement) > 0;
    }


    // 获取符合条件的科研成果
    public List<Achievement> getAchievements(String title, Integer category, String startDate, String endDate) {
        if (title != null && !title.isEmpty()) {
            return achievementMapper.findByTitleLike(title);
        } else if (category != null) {
            return achievementMapper.findByCategory(category);
        } else if (startDate != null && endDate != null) {
            return achievementMapper.findByDateRange(startDate, endDate);
        } else {
            return achievementMapper.findAll();
        }
    }

    // 查询所有待审核的科研成果
    public List<Achievement> getPendingAchievements() {
        return achievementMapper.findByStatus(ContentStatus.PENDING_REVIEW.getCode());
    }

    public boolean updateStatus(Integer achievementId, Integer status) {
        return achievementMapper.updateStatus(achievementId, status) > 0;
    }

    //更新拒绝理由
    public boolean updateRejectReason(Integer achievementId, String rejectReason) {
        return achievementMapper.updateRejectReason(achievementId, rejectReason) > 0;
    }

    public List<Achievement> getAllAchievements() {
        return achievementMapper.findAll();
    }
}
